Skip to content

Add alias and forget commands for user-defined package aliases#23

Open
TitasGailius wants to merge 9 commits into
2.xfrom
feat/aliases-v2
Open

Add alias and forget commands for user-defined package aliases#23
TitasGailius wants to merge 9 commits into
2.xfrom
feat/aliases-v2

Conversation

@TitasGailius

Copy link
Copy Markdown

Creating aliases

Added cpx alias [package] [name] to let users create their own shortcut for a package (e.g. cpx alias laravel/pint pint), prompting interactively via Laravel Prompts for whichever argument is omitted.

Screen Recording 2026-07-01 at 15 00 28

Listing aliases

cpx aliases now also lists user-defined aliases under a "Your aliases" section alongside the built-in ones.

bettershot_1782907426111

Deleting aliases

Added cpx forget [name] to remove a saved alias, with a select prompt listing existing aliases when the name is omitted.

Screen Recording 2026-07-01 at 15 01 49

Other

  • Alias names that collide with a real registered command (list, clean, etc.) are rejected, since they'd never be reachable.
  • User aliases are stored in ~/.cpx/aliases.json and take priority over the built-in aliases at dispatch time, so a user can override e.g. pint to point elsewhere.

Comment thread src/Commands/AliasCommand.php
Comment thread src/Commands/ForgetCommand.php

$json = json_decode((string) file_get_contents($file), true);

return new self(array_map(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A malformed ~/.cpx/aliases.json (invalid JSON, non-array root, or one bad value) throws and breaks every cpx command, including cpx forget, which is how a user would fix it. Let's mirror the is_array() guard already used in Application::resolveVersion(), bail to an empty state otherwise, and skip individual unparseable entries.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WendellAdriel Since aliases.json only gets malformed by hand-editing, bailing to an empty state worries me: the user never really sees the error, and because save() rewrites the whole file, the next alias/forget overwrites their file with the empty state, turning a fixable typo into real data loss.

So I'm wondering, isn't failing loudly with an explicit error more useful and safe approach?

Comment thread src/Packages/UserAliases.php
Comment thread src/Commands/AliasCommand.php
Comment thread src/Commands/AliasCommand.php
@imliam

imliam commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

This looks like it covers the primary use case when a binary matches the package/vendor name clearly, do you think there's appetite for another question/parameter for packages that have multiple binaries when it isn't entirely clear? For example:

"The package vimeo/psalm has multiple binaries, which would you like to alias to?"

  • [psalm]
  • psalter
  • psalm-refactor
  • psalm-review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants